package com.ellfors.common.base

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Modifier

/**
 * Compose 继承这个
 */
abstract class BaseComposeActivity : BaseAndroidActivity() {

    private var mTitle = mutableStateOf("")
    private var mTitleBarVisible = mutableStateOf(true)
    private var mTitleBackVisible = mutableStateOf(true)
    private var mTitleBarBackListener = mutableStateOf<(() -> Unit)?>(null)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
        setContent {
            BasePage()
        }
    }

    @Composable
    abstract fun InitCompose()

    @Composable
    private fun BasePage() {
        Column(modifier = Modifier.fillMaxSize()) {
            BaseComposeTitleBar(
                activity = this@BaseComposeActivity,
                title = mTitle,
                titleBarVisible = mTitleBarVisible,
                backVisible = mTitleBackVisible,
                backListener = mTitleBarBackListener
            )
            InitCompose()
        }
    }

    /*
     ************************ Title Bar *************************
     */
    override fun hideTitleBar() {
        mTitleBarVisible.value = false
    }

    override fun showTitleBar(title: String) {
        mTitleBarVisible.value = true
        mTitle.value = title
    }

    override fun hideTitleBarClose() {
        mTitleBackVisible.value = false
    }

    override fun setTitleBarBackListener(block: () -> Unit) {
        mTitleBarBackListener.value = block
    }
}